package com.youzhi.backend.activiti;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youzhi.backend.dto.CommonConstant;
import com.youzhi.backend.dto.MessageType;
import com.youzhi.backend.entity.Letter;
import com.youzhi.backend.entity.User;
import com.youzhi.backend.exception.EcpEnum;
import com.youzhi.backend.exception.YouZhiException;
import com.youzhi.backend.service.ILetterService;
import com.youzhi.backend.service.IMessageService;
import com.youzhi.backend.service.IUserService;
import com.youzhi.backend.service.IUserSessionService;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @author xumeng
 * @Date 2023/2/13 下午11:14
 * @Description
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Slf4j
@Service("qingJiaManagerUserTaskListener")
public class QingJiaManagerUserTaskListener implements TaskListener {

    @Autowired
    private IMessageService messageService;

    @Autowired
    private IUserSessionService userSessionService;

    @Autowired
    private ILetterService letterService;


    @Autowired
    private IUserService userService;

    @Autowired
    private RuntimeService runtimeService;


    @Override
    public void notify(DelegateTask delegateTask) {
        String assignee = delegateTask.getAssignee();
        log.info("获取审批主管名称:{}", assignee);

        String message = delegateTask.getOwner() + "发起了的请假流程,请审核";

        //发送websocket消息通知到指定用户
        sendWebsocketMessageNotify(assignee, message);
        //写入后台信件表
        writeToLetter(assignee, message);
    }


    /**
     * 写入系统信件表
     *
     * @param assignee
     * @param message
     */
    private void writeToLetter(String assignee, String message) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(User.Fields.name, assignee);
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            throw YouZhiException.newException(EcpEnum.user_not_exist);
        }

        //保存后台信实体
        Letter letter = new Letter();
        letter.setTitle("工作流处理");
        letter.setContent(message);
        letter.setType(ILetterService.workflow_notify);
        letter.setReceiveUserId(user.getId());
        letter.setType(ILetterService.workflow_notify);
        letter.setIsRead(CommonConstant.NO);
        letter.setCreateTime(new Date());
        letter.setUpdateTime(new Date());

        letterService.save(letter);
    }


    /**
     * 发送任务提醒通知
     *
     * @param assignee
     * @param message
     * @return true 发送成功 false用户不在线发送失败
     */
    private boolean sendWebsocketMessageNotify(String assignee, String message) {

        String userWebsocketId = userSessionService.getUserWebsocketId(assignee);

        if (userWebsocketId != null) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("webSocketId", userWebsocketId);
            dataMap.put("message", message);
            messageService.sendMessage(MessageType.workflow_assignee_task_notify, dataMap);
        }

        return true;
    }


}
